package com.cherry.androidviewdemo.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet

/**
 * @author DongMS
 * @since 2020/5/20
 */
class Sample02BeforeOnDrawView : AppCompatTextView {

    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val bound: RectF = RectF()

    constructor (context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
        paint.color = Color.parseColor("#FFC107")
    }

    override fun onDraw(canvas: Canvas) {
        //在onDraw()前绘制
        layout.let {
            bound.left = it.getLineLeft(1)
            bound.top = it.getLineTop(1).toFloat()
            bound.right = it.getLineRight(1)
            bound.bottom = it.getLineBottom(1).toFloat()
        }
        canvas.drawRect(bound, paint)

        layout.let {
            bound.left = it.getLineLeft(lineCount - 4)
            bound.top = it.getLineTop(lineCount - 4).toFloat()
            bound.right = it.getLineRight(lineCount - 4)
            bound.bottom = it.getLineBottom(lineCount - 4).toFloat()
        }
        canvas.drawRect(bound, paint)

        super.onDraw(canvas)
    }

}